﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information

namespace DotNetNuke.Web.Api.Auth.ApiTokens.Models
{
    using System;
    using System.Collections.Generic;

    using DotNetNuke.ComponentModel.DataAnnotations;
    using Newtonsoft.Json;

    /// <summary>
    /// Class that represents an Api Token in the database.
    /// </summary>
    /// <remarks>
    /// This class is used to store data regarding an API token.
    /// </remarks>
    [TableName("ApiTokens")]
    [PrimaryKey("ApiTokenId", AutoIncrement = true)]
    [Scope("PortalId")]
    public class ApiTokenBase
    {
        /// <summary>
        /// Gets or sets the ID of the API token. This is auto generated by SQL.
        /// </summary>
        public int ApiTokenId { get; set; } = -1;

        /// <summary>
        /// Gets or sets a name for the API token.
        /// </summary>
        public string TokenName { get; set; }

        /// <summary>
        /// Gets or sets the hash of the API token. This is used to validate the API token.
        /// </summary>
        [JsonIgnore]
        public string TokenHash { get; set; }

        /// <summary>
        /// Gets or sets the expiration date and time of the API token.
        /// </summary>
        public DateTime ExpiresOn { get; set; }

        /// <summary>
        /// Gets or sets the scope of the API token.
        /// </summary>
        /// <remarks>
        /// The scope is set to determine the API requests allowed by this token.
        /// </remarks>
        public ApiTokenScope Scope { get; set; }

        /// <summary>
        /// Gets or sets the portal ID associated with the API token. This should be -1 if the token is not portal specific (i.e. Host scope).
        /// </summary>
        public int PortalId { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether the token has been revoked.
        /// </summary>
        public bool IsRevoked { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether the token has been deleted.
        /// </summary>
        public bool IsDeleted { get; set; }

        /// <summary>
        /// Gets or sets the user ID who revoked the API token.
        /// </summary>
        public int RevokedByUserId { get; set; }

        /// <summary>
        /// Gets or sets the revocation date and time of the API token.
        /// </summary>
        public DateTime? RevokedOnDate { get; set; }

        /// <summary>
        /// Gets or sets the user ID who created the API token.
        /// </summary>
        public int CreatedByUserId { get; set; }

        /// <summary>
        /// Gets or sets the creation date and time of the API token.
        /// </summary>
        public DateTime CreatedOnDate { get; set; }

        /// <summary>
        /// Gets or sets the time the token was last used.
        /// </summary>
        public DateTime? LastUsedOnDate { get; set; }

        /// <summary>
        /// Gets or sets the list of token keys associated with an API token.
        /// </summary>
        [IgnoreColumn]
        public List<string> TokenKeys { get; set; } = new List<string>();
    }
}
